//
//  UIColor+LCExtension.m
//  LCPackage
//
//  Created by 马军凯 on 2021/6/22.
//  Copyright © 2021 龙采科技集团. All rights reserved.
//

#import "UIColor+LCExtension.h"

@implementation UIColor (LCExtension)

+ (UIColor *)colorWithSixteenBitWithString:(NSString *)string alpha:(CGFloat)alpha {
    NSString *cString = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([cString hasPrefix:@"#"]) {
        cString = [cString substringFromIndex:1];
    }
    if (cString.length < 6) {
        return [UIColor clearColor];
    }
    NSRange range;
    range.location = 0;
    range.length = 2;
    //R、G、B
    NSString *rString = [cString substringWithRange:range];
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:(r / 255.0) green:(g / 255.0) blue:(b / 255.0) alpha:alpha];
}

+ (UIColor *)colorWithSixteenBitWithString:(NSString *)string {
    return [UIColor colorWithSixteenBitWithString:string alpha:1.0];
}

+ (UIColor *)LC_R:(CGFloat)red G:(CGFloat)green B:(CGFloat)blue A:(CGFloat)alpha {
    return [UIColor colorWithRed:(red / 255.0) green:(green / 255.0) blue:(blue / 255.0) alpha:alpha];
}

+ (UIColor *)LC_hex:(NSString *)hex A:(CGFloat)alpha {
    return [UIColor colorWithSixteenBitWithString:hex alpha:alpha];
}

+ (UIColor *)LC_hex:(NSString *)hex {
    return [UIColor colorWithSixteenBitWithString:hex];
}

@end
